#include "../network.h"
#include "../connection_manager.h"
#include "../xml_filetype.h"
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) 
{
	boost::shared_ptr<network::message_queue> q(new network::message_queue);
	network::connection_manager mgr(q);
	boost::shared_ptr<network::i_socket> s(mgr.add_listener(123456));
	while(1) {
		boost::shared_ptr<network::message> msg(q->get());
		cout << msg->type << endl;
		if(msg->type == network::CONNECT) {
			cout << "new connection established" << endl;
			mgr.add_recv(msg->new_sock, 
			boost::shared_ptr<network::filetype>(new 
				network::xml_filetype()));
		} else if(msg->type == network::RECEIVE) {
			cout << "received!" << endl;
			boost::shared_ptr<network::sender> snd(
				mgr.get_sender(msg->sock));
			boost::shared_ptr<model::dom> 
				d(msg->node->insert("inserted"));
			d->insert_attrib("attr1", "val1");
			d->insert_attrib("attr2", "val2");
			d->insert("inserted2")->insert_attrib("attr1", "val1");
			cout << msg->node->to_string() << endl;
			msg->sock->send(snd->send(msg->node));
		} else if(msg->type == network::DELETE) {
			cout << "disconnect" << endl;
		}
	}
	return 0;
}

